/**
 * @author frankle hoo
 * @param exports
 * @returns
 */
layui.define(['form'], function (exports) {
    "use strict";
    
    var MOD_NAME = 'selectPlus',
    	$ = layui.jquery,
    	form = layui.form;
    
    var selectPlus = function() {
    	this.v = '1.0.0';
    }
    
    selectPlus.prototype.render = function(opt) {
    	var elem = $(opt.elem);
    	
    	opt.url = opt.url || ''; //远程请求地址
    	opt.parseData = opt.parseData || undefined; //解析自定义数据
    	opt.response = opt.response || { codeName: 'code', msgName: 'msg', dataName: 'data' }; //默认返回值
    	opt.request = opt.request || { pageName: 'page', limitName: 'limit'}; //默认分页
    	opt.success = opt.success || undefined; //成功之后回调
    	opt.nameField = opt.nameField || 'name'; //显示字段
    	opt.valueField = opt.valueField || 'value'; //值字段
    	opt.where = opt.where || {};
    	opt.data = opt.data || undefined;
    	opt.placeholder = opt.placeholder || '请选择';
    	opt.search = opt.search || false;
    	opt.done = opt.done || undefined;
    	opt.customData = opt.customData || undefined;
    	opt.id = opt.id || '';
    	opt.value = opt.value || '';
    	opt.formatter = opt.formatter || undefined;
    	
    	if(opt.id) {
    		$(elem).attr('lay-filter', opt.id)
    	}
    	
    	if(opt.url) {
    		$.ajax({
    			url: opt.url,
    			type: 'get',
    			async: true,
    			dataType: 'json',
    			data: opt.where,
    			success: function(res) {
    				var data = [];
    				if(opt.parseData) {
    					data = opt.parseData(res);
    				} else {
    					data = res[opt.response.dataName] || [];
    				}
    				
    				if(opt.success) {
    					opt.success(res, elem);
    				}
    				
    				var html = getHtml(data, opt);
    				$(elem).html(html);
    				if(opt.search) {
    					$(elem).attr("lay-search", "")
    				}
    				
    				if(opt.value) {
    					$(elem).val(opt.value)
    				}
    				form.render('select');
    				
    				if(opt.done) {
    					opt.done(elem)
    				}
    			}
    		})
    	} else {
    		var html = getHtml(opt.data, opt);
			$(elem).html(html);
			if(opt.search) {
				$(elem).attr("lay-search", "")
			}
			
			if(opt.value) {
				$(elem).val(opt.value)
			}
			form.render('select');
			
			if(opt.done) {
				opt.done(elem)
			}
    	}
    }
    
    selectPlus.prototype.setData = function(opt, data) {
    	var html = getHtml(data, opt);
		$(elem).html(html);
		form.render('select');
    }
    
    selectPlus.prototype.getData = function(filterId) {
    	return $('select[lay-filter="' + filterId + '"]').val();
    }
    
    selectPlus.prototype.on = function(filterId, callback) {
    	form.on('select(' + filterId + ')', callback);
    }
    
    function getHtml(data, opt) {
    	var html = '<option value="">' + opt.placeholder + '</option>';
    	
    	$(data).each(function(i, c) {
    		html +=  '<option value="' + c[opt.valueField] +'" ';
    		if(opt.customData) {
    			var customDatas = '';
    			$(opt.customData).each(function(index, item) {
    				customDatas += 'data-' + item + '="' + c[item] + '" ';
    			});
    			
    			html += customDatas;
    		}
    		
    		if(opt.formatter) {
    			html += '>' + opt.formatter(c[opt.nameField], c) + '</option>'
    		} else {
    			html += '>' + c[opt.nameField] + '</option>';
    		}
    	})
    	return html;
    }
    
    var selectPlus = new selectPlus();
    
    exports(MOD_NAME, selectPlus);
});